//
//  UIView+YCExtension.m
//  YCPublicDemo
//
//  Created by zhangxiaoliang on 2017/5/17.
//  Copyright © 2017年 xinhuanwangluo. All rights reserved.
//

#import "UIView+YCExtension.h"

typedef NS_ENUM(NSInteger,tipType){
    imageTip = 0,
    buttonTip,
};
@interface TipViewYC : UIView
@property (nonatomic,copy)tipclick tipclick;
-(void)TipClick:(void(^)())tipclick;
@end
@interface TipViewYC()

@property (nonatomic,strong)UILabel *tipLabel;

@property (nonatomic,strong)UIButton *tipButton;

@property (nonatomic,strong)UIImageView *imageView;

@property (nonatomic,assign)tipType type;
@end

@implementation TipViewYC
- (instancetype)initWithFrame:(CGRect)frame Type:(tipType)type
{
    self = [super initWithFrame:frame];
    if (self) {
        UIView *bgView = [[UIView alloc]initWithFrame:frame];
        bgView.backgroundColor = [UIColor whiteColor];
        [self addSubview:bgView];
        _type = type;
    }
    return self;
}
-(UILabel *)tipLabel{
    if (!_tipLabel) {
        _tipLabel = [[UILabel alloc]init];
        _tipLabel.backgroundColor = [UIColor clearColor];
        _tipLabel.textColor = [UIColor lightGrayColor];
        _tipLabel.text = noDataTipText;
        _tipLabel.textAlignment = NSTextAlignmentCenter;
        _tipLabel.font = [UIFont systemFontOfSize:15];
    }
    return _tipLabel;
}
-(UIButton *)tipButton{
    if (!_tipButton) {
        
        _tipButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _tipButton.backgroundColor = [UIColor clearColor];
        UIColor* color = [UIColor colorWithRed: 0.937 green: 0.537 blue: 0.2 alpha: 1];
        [_tipButton setBackgroundColor:color];
        [_tipButton setTitle:buttontitle forState:UIControlStateNormal];
        [_tipButton.layer setMasksToBounds:YES];
        [_tipButton.layer setCornerRadius:3];
        [_tipButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _tipButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [_tipButton addTarget:self
                       action:@selector(btnClick:)
             forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _tipButton;
}
-(UIImageView *)imageView{
    if (!_imageView) {
        _imageView = [[UIImageView alloc]init];
        _imageView.backgroundColor = [UIColor clearColor];
        _imageView.image = [UIImage imageNamed:@"noneDataImage"];
    }
    return _imageView;
}
-(void)layoutSubviews{
    CGFloat centerX = self.superview.frame.size.width/2;
    CGFloat centerY = self.superview.frame.size.height/2;
    
    switch (_type) {
        case imageTip:
        {
            
            self.tipLabel.frame = CGRectMake(0, centerY + 4,self.superview.frame.size.width, 38);
            self.imageView.frame = CGRectMake(centerX- 30, centerY - 4 - 60, 60, 60);
            [self addSubview:self.tipLabel];
            [self addSubview:self.imageView];
            self.tipLabel.text = noDataTipText;
        }
            break;
        case buttonTip:
        {
            self.tipLabel.frame = CGRectMake(0, centerY - 4 - 38,self.superview.frame.size.width, 38);
            self.tipButton.frame = CGRectMake(centerX - 70, centerY + 4, 140, 38);
            [self addSubview:self.tipLabel];
            [self addSubview:self.tipButton];
            self.tipLabel.text = netTipText;
        }
            break;
        default:
            break;
    }
}
- (void)btnClick:(UIButton *)sender
{
    if (_tipclick) {
        _tipclick();
    }
}
-(void)TipClick:(void (^)())tipclick{
    _tipclick = tipclick;
}
@end


@implementation UIView (YCExtension)
-(void)nullTip{
    TipViewYC *tip = [[TipViewYC alloc]initWithFrame:self.bounds Type:imageTip];
    tip.tag = tipViewTag;
    [self addSubview:tip];
}
-(void)errorTip:(tipclick)click{
    TipViewYC *tip = [[TipViewYC alloc]initWithFrame:self.bounds Type:buttonTip];
    tip.tag = tipViewTag;
    tip.tipclick = click;
    [self addSubview:tip];
}
-(void)removeTipView{
    for (UIView *view in self.subviews) {
        if (view.tag == tipViewTag) {
            [view removeFromSuperview];
        }
    }
}
#pragma mark - 设置UIView的X,Y,W,H

-(void)setX:(CGFloat)x
{
    CGRect frame = self.frame;
    frame.origin.x=x;
    self.frame = frame;
    
}


-(void)setY:(CGFloat)y
{
    
    CGRect frame = self.frame;
    frame.origin.y=y;
    self.frame = frame;
}

-(CGFloat)x{
    return self.frame.origin.x;
    
}

-(CGFloat)y
{
    
    return self.frame.origin.y;
}
- (void)setCenterX:(CGFloat)centerX{
    CGPoint point = self.center;
    point.x = centerX;
    self.center = point;
}


- (CGFloat)centerX{
    return self.center.x;
}

- (void)setCenterY:(CGFloat)centerY{
    CGPoint point = self.center;
    point.y = centerY;
    self.center = point;
}


- (CGFloat)centerY{
    return self.center.y;
}

-(void)setWidth:(CGFloat)width
{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

-(void)setHeight:(CGFloat)height
{
    
    CGRect frame = self.frame;
    frame.size.height =height;
    self.frame = frame;
    
    
}
-(CGFloat)width
{
    
    return self.frame.size.width;
}

-(CGFloat)height
{
    return self.frame.size.height;
}

-(void)setSize:(CGSize)size
{
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
    
}

-(CGSize)size
{
    
    return self.frame.size;
}
-(void)setOrigin:(CGPoint)origin
{
    CGRect frame = self.frame;
    frame.origin = origin;
    self.frame = frame;
    
}

-(CGPoint)origin
{
    return self.frame.origin;
}

- (void)setTop:(CGFloat)top {

    CGRect frame = self.frame;
    frame.origin.y = top;
    self.frame = frame;

}

- (CGFloat)top {
    
    return self.frame.origin.y;
}

- (void)setLeft:(CGFloat)left {

    CGRect frame = self.frame;
    frame.origin.x = left;
    self.frame = frame;

}

- (CGFloat)left {

    return self.frame.origin.x;
}

- (void)setBottom:(CGFloat)bottom {
    
    CGRect frame = self.frame;
    frame.origin.y = bottom =  frame.size.height;
    self.frame = frame;
    
}

- (CGFloat)bottom {

    return self.frame.origin.y + self.frame.size.height;
}

- (void)setRight:(CGFloat)right {

    CGRect frame = self.frame;
    frame.origin.x = right - frame.size.width;
    self.frame = frame;

}

- (CGFloat)right {

    return self.frame.origin.x + self.frame.size.width;
}


#pragma mark - nib操作

+ (UINib *)nib
{
    return [self nibWithName:NSStringFromClass([self class])];
}

+ (UINib *)nibWithName:(NSString *)name
{
    return [UINib nibWithNibName:name bundle:[NSBundle mainBundle]];
}

+ (instancetype)loadFromNib
{
    return [self loadFromNib:NSStringFromClass([self class])];
}

+ (instancetype)loadFromNib:(NSString *)nibName
{
    return [[[self nibWithName:nibName] instantiateWithOwner:nil options:nil] firstObject];
}

+ (instancetype)loadFromNibWithFrame:(CGRect)frame
{
    UIView * nibView = [self loadFromNib];
    nibView.frame = frame;
    return nibView;
}

- (void)loadFromNib
{
    UIView * shadow = [[[[self class] nib] instantiateWithOwner:self options:nil] firstObject];

    [self addSubview:shadow];
    
    //给shadow添加自动布局
    shadow.translatesAutoresizingMaskIntoConstraints = NO;
    
    NSLayoutConstraint *xCons = [NSLayoutConstraint constraintWithItem:shadow attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
    NSLayoutConstraint *yCons = [NSLayoutConstraint constraintWithItem:shadow attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
    NSLayoutConstraint *wCons = [NSLayoutConstraint constraintWithItem:shadow attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0];
    NSLayoutConstraint *hCons = [NSLayoutConstraint constraintWithItem:shadow attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0];
    
    [self addConstraints:@[xCons,yCons,wCons,hCons]];
}




#pragma mark - 给视图添加四周边框
- (void)addBorderWithColor:(UIColor *)color size:(CGFloat)size borderTypes:(NSArray *)types{
    for (int i = 0 ; i < types.count; i ++) {
        [self addBorderLayerWithColor:color size:size borderType:[types[i] integerValue]];
    }
}

- (void)addBorderLayerWithColor:(UIColor *)color size:(CGFloat)size borderType:(BorderType)boderType{
    CALayer * layer = [CALayer layer];
    layer.backgroundColor = color.CGColor;
    [self.layer addSublayer:layer];
    
    switch (boderType) {
        case BorderTypeTop:
            layer.frame = CGRectMake(0, 0, self.frame.size.width, size);
            break;
        case BorderTypeLeft:
            layer.frame = CGRectMake(0, 0, size, self.frame.size.height);
            break;
        case BorderTypeBottom:
            layer.frame = CGRectMake(0, self.frame.size.height - size, self.frame.size.width, size);
            break;
        case BorderTypeRight:
            layer.frame = CGRectMake(self.frame.size.width - size, 0, size, self.frame.size.height);
            break;
        default:
            break;
    }
    
}

//+ (UIView *)yc_drawRectWithRoundCorner:(CGFloat)radius borderWidth:(CGFloat)borderWidth backGroundColor:(UIColor *)backGroundColor borderColor:(UIColor *)borderColor {
//
//
//    UIGraphicsBeginImageContextWithOptions(<#CGSize size#>, <#BOOL opaque#>, <#CGFloat scale#>)
//
//}

@end



#pragma mark -  UIView (Maker)的分类实现

@implementation UIView (Maker)

+ (instancetype)yc_instanceWithSuperView:(UIView *)superView constraint:(void (^)(MASConstraintMaker *))constraint {
    UIView *view = [[self alloc]init];
    [superView addSubview:view];
    [view mas_makeConstraints:constraint];
    
    return view;
}

+ (instancetype)yc_instanceWithBackgroundColor:(UIColor *)backgroundColor superView:(UIView *)superView constraint:(void(^)(MASConstraintMaker *))constraint {
    
    UIView *view = [[self alloc]init];
    view.backgroundColor = backgroundColor;
    [superView addSubview:view];
    [view mas_makeConstraints:constraint];
    
    return view;
}

@end
#pragma mark -  UILabel (Maker)的分类实现
@implementation UILabel (Maker)

+ (instancetype)yc_labelWithFontSize:(float)fontSize textColorValue:(uint)textColorValue superView:(UIView *)superView constraint:(void(^)(MASConstraintMaker *))constraint {
    return [UILabel yc_labelWithFont:[UIFont systemFontOfSize:fontSize] textColor:[UIColor colorWithRGBValue:textColorValue] superView:superView constraint:constraint];
}

+ (instancetype)yc_labelWithFont:(UIFont *)font textColor:(UIColor *)textColor superView:(UIView *)superView constraint:(void(^)(MASConstraintMaker *))constraint {
    return [UILabel yc_labelWithText:nil font:font textColor:textColor backgroundColor:nil superView:superView constraint:constraint];
}

+ (instancetype)yc_labelWithText:(NSString *)text fontSize:(float)fontSize textColorValue:(uint)textColorValue backgroundColorValue:(uint)backgroundColorValue superView:(UIView *)superView constraint:(void(^)(MASConstraintMaker *))constraint {
    
    return [UILabel yc_labelWithText:text font:[UIFont systemFontOfSize:fontSize] textColor:[UIColor colorWithRGBValue:textColorValue] backgroundColor:[UIColor colorWithRGBValue:backgroundColorValue] superView:superView constraint:constraint];
}

+ (instancetype)yc_labelWithText:(NSString *)text font:(UIFont *)font textColor:(UIColor *)textColor backgroundColor:(UIColor *)backgroundColor superView:(UIView *)superView constraint:(void(^)(MASConstraintMaker *))constraint {
    
    UILabel *label = [[UILabel alloc]init];
    label.text = text;
    label.font = font;
    label.textColor = textColor;
    label.backgroundColor = backgroundColor;
    [superView addSubview:label];
    [label mas_makeConstraints:constraint];
    
    return label;
}

@end
#pragma mark -  UIButton (Maker)的分类实现
@implementation UIButton (Maker)

+ (instancetype)yc_buttonWithTitle:(NSString *)title titleColor:(UIColor *)titleColor titleFont:(UIFont *)titleFont superView:(UIView *)superView constraint:(void(^)(MASConstraintMaker *))constraint {
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    button.titleLabel.font = titleFont;
    [superView addSubview:button];
    [button mas_makeConstraints:constraint];
    
    return button;
    
}

+ (instancetype)yc_buttonWithSuperView:(UIView *)superView constraint:(void(^)(MASConstraintMaker *))constraint {
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [superView addSubview:button];
    [button mas_makeConstraints:constraint];
    
    return button;
}

+ (instancetype)yc_buttonWithImage:(UIImage *)image selectImage:(UIImage *)selectImage superView:(UIView *)superView constraint:(void (^)(MASConstraintMaker *))constraint {
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:image forState:UIControlStateNormal];
    [button setImage:selectImage forState:UIControlStateSelected];
    [superView addSubview:button];
    [button mas_makeConstraints:constraint];
    
    return button;
    
}

@end

#pragma mark -  UITableView (Maker)的分类实现

static const void *k_yc_tableView_sectionNumber = @"k_yc_tableView_sectionNumber";
static const void *k_yc_tableView_rowNumber = @"k_yc_tableView_rowNumber";
static const void *k_yc_tableView_cellConfig = @"k_yc_tableView_cellConfig";
static const void *k_yc_tableView_iden = @"k_yc_tableView_iden";

@interface UITableView (PropertyExtension)

@property (copy, nonatomic) NSInteger (^sectionNumber)(void);
@property (copy, nonatomic) NSInteger (^rowNumber)(NSInteger section);
@property (copy, nonatomic) void (^cellConfig)(NSIndexPath *indexPath ,UITableViewCell *cell);
@property (strong, nonatomic) NSString *iden;

@end

@implementation UITableView (Maker)

+ (instancetype)yc_tableViewWithStyle:(UITableViewStyle)style cellClassOrNibName:(id)cellClassOrNibName sectionNumber:(NSInteger (^)(void))sectionNumber rowNumber:(NSInteger (^)(NSInteger section))rowNumber cellConfig:(void (^)(NSIndexPath *indexPath ,UITableViewCell *cell))cellConfig {
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectZero style:style];
    
    tableView.dataSource = tableView;
    if ([cellClassOrNibName isKindOfClass:[NSString class]]) {
        [tableView registerNib:[UINib nibWithNibName:cellClassOrNibName bundle:nil] forCellReuseIdentifier:cellClassOrNibName];
        tableView.iden = (NSString *)cellClassOrNibName;
    } else if ((Class)cellClassOrNibName) {
        [tableView registerClass:cellClassOrNibName forCellReuseIdentifier:NSStringFromClass(cellClassOrNibName)];
        tableView.iden = NSStringFromClass(cellClassOrNibName);
    } else if (!cellClassOrNibName){
        [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"DefaultUITableViewCell"];
        tableView.iden = @"DefaultUITableViewCell";
    }
    tableView.sectionNumber = sectionNumber;
    tableView.rowNumber = rowNumber;
    tableView.cellConfig = cellConfig;
    
    return tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (!self.rowNumber) {
        return 0;
    }
    return self.rowNumber(section);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.iden forIndexPath:indexPath];
    self.cellConfig(indexPath,cell);
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (!self.sectionNumber) {
        return 1;
    }
    return self.sectionNumber();
}


- (void)setIden:(NSString *)iden {
    objc_setAssociatedObject(self, k_yc_tableView_iden, iden, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void)setSectionNumber:(NSInteger (^)(void))sectionNumber {
    objc_setAssociatedObject(self, k_yc_tableView_sectionNumber, sectionNumber, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void)setRowNumber:(NSInteger (^)(NSInteger))rowNumber {
    objc_setAssociatedObject(self, k_yc_tableView_rowNumber, rowNumber, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void)setCellConfig:(void (^)(NSIndexPath *, UITableViewCell *))cellConfig {
    objc_setAssociatedObject(self, k_yc_tableView_cellConfig, cellConfig, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (NSString *)iden {
    return objc_getAssociatedObject(self, k_yc_tableView_iden);
}

- (NSInteger (^)(void))sectionNumber {
    return objc_getAssociatedObject(self, k_yc_tableView_sectionNumber);
}

- (NSInteger (^)(NSInteger))rowNumber {
    return objc_getAssociatedObject(self, k_yc_tableView_rowNumber);
}

- (void (^)(NSIndexPath *, UITableViewCell *))cellConfig {
    return objc_getAssociatedObject(self, k_yc_tableView_cellConfig);
}




@end


